package mipo.top.shop.service.impl;

import mipo.top.base.exception.BusinessException;
import mipo.top.base.service.impl.BaseServiceimpl;
import mipo.top.pet.Service.IPetService;
import mipo.top.pet.domain.Pet;
import mipo.top.shop.Dto.ConfirmDto;
import mipo.top.shop.domain.OrderPetAcquisition;
import mipo.top.shop.domain.SearchMasterMsg;
import mipo.top.shop.mapper.OrderPetAcquisitionMapper;
import mipo.top.shop.service.IOrderPetAcquisitionService;
import mipo.top.shop.service.ISearchMasterMsgService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

@Service
public class OrderPetAcquisitionServiceimpl extends BaseServiceimpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private ISearchMasterMsgService searchMasterMsgService;
    @Autowired
    private IPetService petService;

    @Override
    public void confirmOrder(ConfirmDto confirmDto) {
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.findone(confirmDto.getId());
        if (orderPetAcquisition==null){
            throw new BusinessException("订单不存在");
        }
        
        SearchMasterMsg searchMasterMsg = searchMasterMsgService.findone(orderPetAcquisition.getSearchMasterMsgId());
        if (searchMasterMsg==null){
            throw new BusinessException("订单不存在");
        }
        searchMasterMsg.setState(3);
        searchMasterMsgService.update(searchMasterMsg);

        Pet pet = getPet(orderPetAcquisition, searchMasterMsg);

        orderPetAcquisition.setState(1);
        orderPetAcquisition.setPrice(new BigDecimal(confirmDto.getMoney()));
        orderPetAcquisition.setPetId(pet.getId());
        orderPetAcquisitionMapper.update(orderPetAcquisition);
    }

    @NotNull
    private Pet getPet(OrderPetAcquisition orderPetAcquisition, SearchMasterMsg searchMasterMsg) {
        Pet pet = new Pet();
        pet.setName(searchMasterMsg.getName());
        pet.setCreatetime(new Date());
        pet.setUserId(orderPetAcquisition.getUserId());
        pet.setShopId(orderPetAcquisition.getShopId());
        pet.setSearchMasterMsgId(searchMasterMsg.getId());
        pet.setCostprice(searchMasterMsg.getPrice().toString());
        pet.setTypeId(1L);
        petService.save(pet);
        return pet;
    }

    @Override
    public void cancel(Long id) {
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.findone(id);
        if (orderPetAcquisition==null){
            throw new BusinessException("订单不存在");
        }
        orderPetAcquisition.setState(-1);
        orderPetAcquisitionMapper.update(orderPetAcquisition);
        SearchMasterMsg searchMasterMsg = searchMasterMsgService.findone(orderPetAcquisition.getSearchMasterMsgId());
        if (searchMasterMsg==null){
            throw new BusinessException("订单不存在");
        }
        searchMasterMsg.setState(1);
        searchMasterMsg.setShopId(null);
        searchMasterMsgService.update(searchMasterMsg);

    }
}
